package com.dxl.system.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dxl.system.mapper.ConfigMapper;
import com.dxl.system.service.ConfigService;
import com.dxl.system.model.Config;
import com.dxl.base.Result;
import com.dxl.base.Results;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * <b> 系统设置服务接口实现类
 * </b><br><br><i>Description</i> :
 * <br>Date: 2019-10-03    <br>Author : dxl
 */
@Service
public class ConfigServiceImpl extends ServiceImpl<ConfigMapper,Config> implements ConfigService {

    @Autowired(required = false)
    private ConfigMapper configMapper;

    /**
     * <b> 插入系统设置
     * </b><br><br><i>Description</i> : 不能为空：键-config_key 值-config_value 配置名称-config_name ；不能重复：键-config_key  
     * @param config Config
     * @return com.dxl.base.Result Config
     * <br><br>Date: 2019-10-03    <br>Author : dxl
     */
    @Override
    public Result addOne(Config config) {
        Result result = isConfigAddable(config);
        boolean flag = (boolean) result.getData();
        if(!flag){
            return Results.failure(result.getMsg());
        }
        configMapper.insert(config);
        return Results.successWithData(config);
    }

    /**
     * <b> 删除系统设置
     * </b><br><br><i>Description</i> : 
     * @param configIds List: Integer
     * @return com.dxl.base.Result int
     * <br><br>Date: 2019-10-03    <br>Author : dxl
     */
    @Override
    public Result delByIds(List<Integer> configIds) {
        int num = configMapper.deleteBatchIds(configIds);
        return Results.successWithData(num);
    }


    /**
     * <b> 编辑系统设置
     * </b><br><br><i>Description</i> : 不能重复：键-config_key 
     * @param config Config
     * @return com.dxl.base.Result Config
     * <br><br>Date: 2019-10-03    <br>Author : dxl
     */
    @Override
    public Result editOne(Config config) {
        Result result = isConfigEditable(config);
        boolean flag = (boolean) result.getData();
        if(!flag){
            return Results.failure(result.getMsg());
        }
        configMapper.updateById(config);
        return Results.successWithData(config);
    }

    /**
     * <b> ID查询系统设置
     * </b><br><br><i>Description</i> : 
     * @param configId Integer
     * @return com.dxl.base.Result: Config
     * <br><br>Date: 2019-10-03    <br>Author : dxl
     */
    @Override
    public Result getById(Integer configId) {
        Config config = configMapper.selectById(configId);
        return Results.successWithData(config);
    }

    /**
     * <b> 条件查询系统设置
     * </b><br><br><i>Description</i> : 
     * @param config Config
     * @return com.dxl.base.Result List: Config
     * <br><br>Date: 2019-10-03    <br>Author : dxl
     */
    @Override
    public Result getList(Config config) {
        QueryWrapper<Config> configWrapper = new QueryWrapper<Config>(config);
        List<Config> configList = configMapper.selectList(configWrapper);
        return Results.successWithData(configList);
    }

    /**
     * <b> 分页条件查询系统设置
     * </b><br><br><i>Description</i> : 
     * @param config Config, currentPage -Integer, pageSize -Integer
     * @return com.dxl.base.Result List: Config
     * <br><br>Date: 2019-10-03     <br>Author : dxl
     */
    @Override
    public Result getListByPage(Config config, Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        List<Config> configList = (List<Config>) getList(config).getData();
        PageInfo<Config> pageInfo = new PageInfo<Config>(configList);
        return Results.successWithData(pageInfo);
    }

    /**
     * <b> 系统设置能否新增
     * </b><br><br><i>Description</i> : 不能为空：键-config_key 值-config_value 配置名称-config_name ；不能重复：键-config_key  
     * @param config Config
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-10-03     <br>Author : dxl
     */
    @Override
    public Result isConfigAddable(Config config) {

        if(config.getConfigKey() == null){
            return Results.successWithData(false,"键 不能为空:(");
        }

        if(config.getConfigValue() == null){
            return Results.successWithData(false,"值 不能为空:(");
        }

        if(config.getConfigName() == null){
            return Results.successWithData(false,"配置名称 不能为空:(");
        }

        if(config.getConfigKey() != null){
        QueryWrapper<Config> configWrapper = new QueryWrapper<>();
        configWrapper.eq("config_key",config.getConfigKey());
        List<Config> configList = configMapper.selectList(configWrapper);
            if(configList.size() > 0){
                return Results.successWithData(false,"键 重复:(");
            }
        }

        return Results.successWithData(true,"无重复:)");
    }

    /**
     * <b> 系统设置能否编辑
     * </b><br><br><i>Description</i> : 不能重复：键-config_key 
     * @param config Config
     * @return com.dxl.base.Result boolean
     * <br><br>Date: 2019-10-03     <br>Author : dxl
     */
    @Override
    public Result isConfigEditable(Config config) {

        if(config.getConfigKey() != null){
            QueryWrapper<Config> configWrapper1 = new QueryWrapper<>();
            configWrapper1.eq("id",config.getId()).eq("config_key",config.getConfigKey());
            List<Config> configList1 = configMapper.selectList(configWrapper1);
            QueryWrapper<Config> configWrapper2 = new QueryWrapper<>();
            configWrapper2.eq("config_key",config.getConfigKey());
            List<Config> configList2 = configMapper.selectList(configWrapper2);
            if(!((configList1.size() == 1 && configList2.size() == 1) ||(configList1.size() == 0 && configList2.size() == 0))){
                return Results.successWithData(false,"键 重复:(");
            }
        }

        return Results.successWithData(true,"无重复:)");
    }
}
